package csel.controller.commands;

import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.JOptionPane;

import csel.model.Model;
import csel.model.GameCommand;
import csel.view.Viewport;

public class SaveCommand implements GameCommand
{
	private Model model;
	private Viewport viewport;
	private String fileName;
	
	//construct new save command
	public SaveCommand(Model model, Viewport viewport)
	{
		this.model = model;
		this.viewport = viewport;
		this.fileName = viewport.promptSaveDialog();
	}
	
	//call the model method save with the given filename after checking if it is null or not
	public void execute()
	{
		if (fileName != null)
		{
			try{
				model.save(fileName);
			}
			catch(FileNotFoundException e)
			{
				System.out.println(e.toString());
			//	e.printStackTrace();			//for the testing path
				//hahaha I caught you
				JOptionPane.showMessageDialog(null, "File not found. Please find the right file this time.");
			} 
			//doing the catching IOException
			catch (IOException e) {
				System.out.println(e.toString());
		//		e.printStackTrace();		//for the testing path
				//hahaha I catch you too
				JOptionPane.showMessageDialog(null, "Couldn't read your file. Please check that file");
			}
		}
		if(Model.infoMode)System.out.println("[MODEL] the Save command has been called");
	}
	
	public String toString()
	{
		return this.getClass().getSimpleName();
	}

}